package cn.donting.jme.expansion.file;

import cn.donting.jme.EditorContent;
import cn.donting.jme.expansion.attribute.AttributeGroup;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.inspect.InspectBuilder;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.swing.tab.InspectTab;
import cn.hutool.core.io.FileUtil;
import com.jme3.asset.AssetManager;
import com.jme3.asset.MaterialKey;
import com.jme3.material.Material;

import java.io.File;
import java.util.List;

@Bean
public class J3mOpenFile implements OpenFile {
    @Override
    public boolean isOpen(File file, String extName) {
        return extName.equals("j3m");
    }

    @Override
    public void open(File file) throws Exception {
        String name = FileUtil.subPath(EditorContent.getProject().getSrcResourcesDir().getCanonicalPath(),
                file.getCanonicalPath());

        Material material = EditorContent.getAssetManagerFromCache(new MaterialKey(name));
        InspectTab inspectTab = EditorContent.getEditorUI().getTabOfAdd(InspectTab.tabIndex, InspectTab.class);
        EditorContent.submitTask(() -> {
            Inspect inspect = InspectBuilder.create(material);
            inspectTab.setAttributeGroup(inspect);
        }, ex -> {
            inspectTab.setLoadError(ex);
        });

    }
}
